/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.cashmate.struts;

import com.cashmate.hibernate.pojo.Employees;
import com.cashmate.hibernate.pojo.Usertypes;
import com.cashmate.utilities.IncentiveObject;
import com.opensymphony.xwork2.ActionSupport;
import java.util.Comparator;
import java.util.LinkedList;
import java.util.List;
import org.apache.log4j.Logger;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;

/**
 *
 * @author dean
 */
@Result(name = "error", type = "redirect", location = "/error/messages.jsp")
public class GridIncentives extends ActionSupport {

    private static final Logger logger = Logger.getLogger(GridIncentives.class);
    private List<IncentiveObject> gridModel;
    private Integer rows = 0;
    private Integer page = 0;
    private String sord;
    private String sidx;
    private String searchField;
    private String searchString;
    private String searchOper;
    private Integer total = 0;
    private Integer records = 0;
    private Employees emps = (Employees) ServletActionContext.getRequest().getSession().getAttribute("employee");
    private Usertypes ut = (Usertypes) ServletActionContext.getRequest().getSession().getAttribute("usertype");

    @Action(value = "/grid-incentives", results = {
        @Result(name = "success", type = "json")
    })
    @Override
    public String execute() throws Exception {
        try {
            List<IncentiveObject> temp = (List<IncentiveObject>) ServletActionContext.getRequest().getSession().getAttribute("incentiveList");
            if (temp != null && temp.size() > 0) {
                gridModel = new LinkedList<IncentiveObject>();
                QueryPrivilege2 priv = new QueryPrivilege2();
                priv.queryPrivilege(14, ut.getUsertypeid());
                if (!priv.isViewable()) {
                    for (IncentiveObject i : temp) {
                        if (!i.getAreacode().equals("HO") && !i.isSecond()) {
                            gridModel.add(i);
                        }
                    }
                }else{
                    gridModel = temp;
                }
            }
            records = gridModel.size();
            total = records;

            return SUCCESS;
        } catch (Exception ex) {
            Employees emps = (Employees) ServletActionContext.getRequest().getSession().getAttribute("employee");
            if (emps != null) {
                logger.fatal("(" + emps.getIdnumber() + "):" + ex.getLocalizedMessage(), ex);
            } else {
                logger.fatal(ex.getLocalizedMessage(), ex);
            }
            ex.printStackTrace();
            addActionError(ex.getLocalizedMessage());
            return ERROR;
        }
    }

    public List<IncentiveObject> getGridModel() {
        return gridModel;
    }

    public void setGridModel(List<IncentiveObject> gridModel) {
        this.gridModel = gridModel;
    }

    public Integer getPage() {
        return page;
    }

    public void setPage(Integer page) {
        this.page = page;
    }

    public Integer getRecords() {
        return records;
    }

    public void setRecords(Integer records) {
        this.records = records;
    }

    public Integer getRows() {
        return rows;
    }

    public void setRows(Integer rows) {
        this.rows = rows;
    }

    public String getSearchField() {
        return searchField;
    }

    public void setSearchField(String searchField) {
        this.searchField = searchField;
    }

    public String getSearchOper() {
        return searchOper;
    }

    public void setSearchOper(String searchOper) {
        this.searchOper = searchOper;
    }

    public String getSearchString() {
        return searchString;
    }

    public void setSearchString(String searchString) {
        this.searchString = searchString;
    }

    public String getSidx() {
        return sidx;
    }

    public void setSidx(String sidx) {
        this.sidx = sidx;
    }

    public String getSord() {
        return sord;
    }

    public void setSord(String sord) {
        this.sord = sord;
    }

    public Integer getTotal() {
        return total;
    }

    public void setTotal(Integer total) {
        this.total = total;
    }
}

class IncentiveObjectComparator implements Comparator<IncentiveObject> {

    private String token;

    public IncentiveObjectComparator(String token) {
        this.token = token;
    }

    public int compare(IncentiveObject t, IncentiveObject t1) {
        if (token == null || token.length() < 1) {
            return 0;
        } else if (token.equals("rate_s")) {
            return diffDouble(t.getRate(), t1.getRate());
        } else if (token.equals("gross")) {
            return diffDouble(t.getGross(), t1.getGross());
        } else if (token.equals("officeShare")) {
            return diffDouble(t.getOfficeShare(), t1.getOfficeShare());
        } else if (token.equals("projIncentive")) {
            return diffDouble(t.getProjIncentive(), t1.getProjIncentive());
        } else if (token.equals("sales")) {
            return diffDouble(t.getSales(), t1.getSales());
        } else if (token.equals("actIncentive")) {
            return diffDouble(t.getActIncentive(), t1.getActIncentive());
        } else if (token.equals("payableIncentive")) {
            return diffDouble(t.getPayableIncentive(), t1.getPayableIncentive());
        } else if (token.equals("penalty")) {
            return diffDouble(t.getPenalty(), t1.getPenalty());
        } else if (token.equals("nm")) {
            return diffDouble(t.getNm(), t1.getNm());
        } else if (token.equals("cc")) {
            return t.getCc().compareTo(t1.getCc());
        } else {
            return t.getAreacode().compareTo(t1.getAreacode());
        }
    }

    private int diffDouble(double d1, double d2) {
        if (d1 > d2) {
            return 1;
        } else if (d1 < d2) {
            return -1;
        } else {
            return 0;
        }
    }
}
